<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Paginator\Paginator;
use Zend\Db\Sql\Select;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Application\Model\Category;
use Admin\Form\MenuForm;
use Admin\Form\NewsForm;
use Application\Model\vn3dUtil;

class CategoryController extends AbstractActionController
{

    protected $CategoryTable;

    public function getCategoryTable()
    {
        if (! $this->CategoryTable) {
            $sm = $this->getServiceLocator();
            $this->CategoryTable = $sm->get('Application\Model\CategoryTable');
        }
        return $this->CategoryTable;
    }

    public function indexAction()
    {
        if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $order_by = $this->params()->fromRoute('order_by') ? $this->params()->fromRoute('order_by') : 'id';
        $order = $this->params()->fromRoute('order') ? $this->params()->fromRoute('order') : Select::ORDER_ASCENDING;
        $page = $this->params()->fromRoute('page') ? (int) $this->params()->fromRoute('page') : 1;
        // grab the paginator from the AlbumTable
        $paginator = $this->getCategoryTable()->fetchAll(true);
        // set the current page to what has been passed in query string, or to 1 if none set
        $paginator->setCurrentPageNumber((int) $this->params()
            ->fromQuery('page', $page));
        // set the number of items per page to 10
        $paginator->setItemCountPerPage(5);

        $this->setLayoutAdmin($this);
        return new ViewModel(array(
            'categories' => $this->getCategoryTable()->getMenu()
        ));
    }

    public function editAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $id = (int) $this->params()->fromRoute('id', 0);

        $menuEdit = $this->getCategoryTable()->getCategorybyId($id);

        if (empty($menuEdit)) {
            // Redirect to list of albums
            return $this->redirect()->toRoute('category', array(
                "action" => "index"
            ));
        }
        $form = new MenuForm("menu");
        $form->get('submit')->setAttribute('value', 'edit');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $menu = new Category();
            $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
            $form->setInputFilter($menu->getInputFilter());
            $form->setData($data);

            // if ($form->isValid()) {
            $menu->id=$id;
            $menu->name = $data['name'];
            $menu->desc = $data['desc'];

            // $location->exchangeArray($request->getPost());
            $this->getCategoryTable()->saveCategory($menu);

            // Redirect to list of albums
            return $this->redirect()->toRoute('category', array(
                "action" => "index"
            ));
            // }
        }

        $this->setLayoutAdmin($this);
        return array(
            'form' => $form,
            "menuEdit"=>$menuEdit,
            "id"=>$id
        );
    }
    public function editGroupProductAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $id = (int) $this->params()->fromRoute('id', 0);
        $parentID = (int) $this->params()->fromRoute('urlseo', 0);
        $menuEdit = $this->getCategoryTable()->getCategorybyId($id);

        if (empty($menuEdit)) {
            // Redirect to list of albums
            return $this->redirect()->toRoute('category', array(
                "action" => "group-product",
                "id"=>$parentID
            ));
        }
        $form = new MenuForm("menu");
        $form->get('submit')->setAttribute('value', 'edit');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $menu = new Category();
            $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
            $form->setInputFilter($menu->getInputFilter());
            $form->setData($data);

            // if ($form->isValid()) {
            $menu->id=$id;
            $menu->name = $data['name'];
            $menu->desc = $data['desc'];
            $menu->parent_id=$parentID;

            // $location->exchangeArray($request->getPost());
            $this->getCategoryTable()->saveCategory($menu);

            // Redirect to list of albums
            return $this->redirect()->toRoute('category', array(
               "action" => "group-product",
                "id"=>$parentID
            ));
            // }
        }
        $this->setLayoutAdmin($this);
        return array(
            'form' => $form,
            "menuEdit"=>$menuEdit,
            "id"=>$id,
            "parentID"=>$parentID
        );
    }

    public function deleteAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $id = (int) $this->params()->fromRoute('id', 0);
        if (! $id) {
            return $this->redirect()->toRoute('category', array(
                "action" => "index"
            ));
        }
        $this->getCategoryTable()->deleteCategory($id);
        return $this->redirect()->toRoute('category', array(
            "action" => "index"
        ));

        return array(
            'id' => $id
        );
    }


    public function deleteGroupProductAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $id = (int) $this->params()->fromRoute('id', 0);
        $parentID = (int) $this->params()->fromRoute('urlseo', 0);
        if (! $id) {
            return $this->redirect()->toRoute('category', array(
                "action" => "group-product",
                "id" => $parentID
            ));
        }
        $this->getCategoryTable()->deleteCategory($id);
        return $this->redirect()->toRoute('category', array(
            "action" => "group-product",
            "id" => $parentID
        ));

        return array(
            'id' => $id,
            "id" => $parentID
        );
    }

    public function addAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $form = new MenuForm("menu");
        $form->get('submit')->setAttribute('value', 'Add');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $menu = new Category();
            $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
            $form->setInputFilter($menu->getInputFilter());
            $form->setData($data);

            // if ($form->isValid()) {

            $menu->name = $data['name'];
            $menu->desc = $data['desc'];

            // $location->exchangeArray($request->getPost());
            $this->getCategoryTable()->saveCategory($menu);

            // Redirect to list of albums
            return $this->redirect()->toRoute('category', array(
                "action" => "index"
            ));
            // }
        }
        $this->setLayoutAdmin($this);
        return array(
            'form' => $form
        );
    }

    public function addGroupProductAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $parentID = $this->getEvent()
            ->getRouteMatch()
            ->getParam("id");
        $form = new MenuForm("menu");
        $form->get('submit')->setAttribute('value', 'Add');
        $request = $this->getRequest();
        if ($request->isPost()) {
            $menu = new Category();
            $data = array_merge_recursive($request->getPost()->toArray(), $request->getFiles()->toArray());
            $form->setInputFilter($menu->getInputFilter());
            $form->setData($data);

            // if ($form->isValid()) {

            $menu->name = $data['name'];
            $menu->desc = $data['desc'];
            $menu->parent_id = $parentID;

            // $location->exchangeArray($request->getPost());
            $this->getCategoryTable()->saveCategory($menu);

            // Redirect to list of albums
            return $this->redirect()->toUrl(vn3dUtil::DOMAIN . "/admin/category/group-product/$parentID");
            // }
        }
        $this->setLayoutAdmin($this);
        return array(
            'form' => $form,
            "parentID" => $parentID
        );
    }

    public function groupProductAction()
    {if(!vn3dUtil::checkAuth(($this))){

        	return $this->redirect()->toRoute('user',array("action"=>"index"));
        };
        $parentID = $this->getEvent()
            ->getRouteMatch()
            ->getParam("id");
        $this->setLayoutAdmin($this);
        return new ViewModel(array(
            'categories' => $this->getCategoryTable()->getGroupProduct($parentID),
            "parentID" => $parentID
        ));
    }

    function after($parten, $inthat)
    {
        if (! is_bool(strpos($inthat, $parten)))
            return substr($inthat, strpos($inthat, $parten) + strlen($parten));
    }

    function strrevpos($instr, $needle)
    {
        $rev_pos = strpos(strrev($instr), strrev($needle));
        if ($rev_pos === false)
            return false;
        else
            return strlen($instr) - $rev_pos - strlen($needle);
    }

    function after_last($parten, $inthat)
    {
        if (! is_bool($this->strrevpos($inthat, $parten)))
            return substr($inthat, $this->strrevpos($inthat, $parten) + strlen($parten));
    }
    private function  setLayoutAdmin($controller){
        $controller->layout("/layout/admin");
        $controller->forward()->dispatch("Application\Controller\Store", array(
        		'action' => 'search',
        		'controller' => 'Application\Controller\Store'
        ));
    }
}
